<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>地图定位</title>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    <script src="http://webapi.amap.com/maps?v=1.3&key=cb1f326b08e1017c4d97b29a0c39cebd&plugin=AMap.Autocomplete,AMap.Geocoder"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<div id="myPageTop">
    <table>
        <tr>
            <td>
                <label>按关键字搜索：</label>
            </td>
            <!-- <td class="column2">
                <label>左击获取经纬度：</label>
            </td> -->
        </tr>
        <tr>
            <td>
                <input type="text" placeholder="请输入关键字进行搜索" id="tipinput">
            </td>
            <td class="column2">
                <input type="hidden" readonly="true" id="lng">
                <input type="hidden" readonly="true" id="lat">
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    var lat = {$lat} ? {$lat} : '39.908824';
    var lng = {$lng} ? {$lng} : '116.397391';
    var map = new AMap.Map("container", {
        resizeEnable: true,
        center: [lng, lat],
        zoom: 13
    });
    var markers = [];
    if (lat && lng) {
        lnglatXY = [lng, lat]; //已知点坐标
        marker = new AMap.Marker({  //加点
            map: map,
            position: lnglatXY,
        });
        markers.push(marker);
    };

    function regeocoder(lng, lat) {  //逆地理编码
        lnglatXY = [lng, lat];
        var geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all"
        });        
        map.remove(markers);
        marker = new AMap.Marker({  //加点
            map: map,
            position: lnglatXY,
        });
        markers.push(marker);
    }
    
    //为地图注册click事件获取鼠标点击出的经纬度坐标
    var clickEventListener = map.on('click', function(e) {
        regeocoder(e.lnglat.getLng(), e.lnglat.getLat());
        document.getElementById("lng").value = e.lnglat.getLng();
        document.getElementById("lat").value = e.lnglat.getLat();
    });
    var auto = new AMap.Autocomplete({
        input: "tipinput"
    });
    AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
    function select(e) {
        if (e.poi && e.poi.location) {
            map.setZoom(15);
            map.setCenter(e.poi.location);
        }
    }
</script>
</body>
</html>